package com.major.dawn.mit.sum;

import java.util.function.Function;

/**
 * @author qiaobao
 * @since --
 */
public class SumTerm {

    public static int inc(int a ){
        return a + 1;
    }

    public static int cube(int a){
        return a * a * a;
    }

    public static int sumCube(Function<Integer, Integer> term, int a, Function<Integer, Integer> next, int b) {
        if (a > b) {
            return 0;
        }

        return term.apply(a) + sumCube(term, next.apply(a), next, b);
    }

    public static int sumCubeIter(Function<Integer, Integer> term, int a, Function<Integer, Integer> next, int b) {
        int result = 0;
        for (int i = a; i <= b; i = next.apply(i)) {
            result = term.apply(i) + result;
        }
        return result;
    }




    public static void main(String[] args) {
        Function<Integer, Integer> cube = (a) -> a * a * a;
        Function<Integer, Integer> inc = (a) -> a + 1;
        int sumCube = sumCube(cube, 3, inc, 4);
        System.out.println(sumCube);
        int cubeIter = sumCubeIter(cube, 3, inc, 4);
        System.out.println(cubeIter);


    }


}
